const app = getApp();
const {upTime} = require('/util/public');
Page({
    data: {
        config: app.config,
        order:[
            {title:"待付款",icon:`https://oss.yinuobao.cn/backstage/2022-08-02/2982eef257fc41b39e2759dfcec82371.png`,index:1,markNum:0},
            {title:"待发货",icon:`https://oss.yinuobao.cn/backstage/2022-08-02/b8cb0c0b6b3d4cba909c2c0c7ad8616c.png`,index:4,markNum:0},
            {title:"待收货",icon:`https://oss.yinuobao.cn/backstage/2022-08-02/3b14661b82b64a5d8c264a423a3b9323.png`,index:5,markNum:0},
            {title:"已完成",icon:`https://oss.yinuobao.cn/backstage/2022-08-02/4e9fca65f9d945e68d43429a4d2ba3ba.png`,index:7,markNum:0},
        ],
        server:[
            // {title:"商家入驻",icon:`${app.config.image}/my_icon5.png`,tab:'navigateTo',url:"/pages/user/enter/enter"},
            {title:"身份认证",icon:`../../../image/my/people.png`,tab:'navigateTo',url:"/pages/user/authentication/authentication"},
            {title:"收货地址",icon:`https://oss.yinuobao.cn/backstage/2022-08-02/5a8c8c19520a461780e271d0d85ebe5c.png`,tab:'address'},
            {title:"在线客服",icon:`https://oss.yinuobao.cn/backstage/2022-08-02/8601ee60e9f348d19d905ea08725878e.png`,tab:'server'},
        ],
        banner:`${app.config.image}/my_ruzhu.png`,
        monthBillBc: `${app.config.image}/monthBill_bc_img.png`,
        loginShow:true,
        login:false,

        prudects:[],

        noPayStatusNum: 0, //待付款订单数量
        noDeliverStatusNum: 0, //待发货订单数量
        noSignStatusNum: 0, //待收货订单数量

        recentlyBill: null,

        params: [
          {
            activityId: '2022051100826004950115286127',
            outBizNo: 'adeb91bb023041279da5d03d5e86c977',
          },
        ],
        senderMerchantId: '2088341688201953',
        dialogBtnType: 1,
        dialogBtnStyle: {
          color: '#fff',
          borderColor: '#1664FF',
          backgroundColor: '#1664FF',
        },

        promoteId: '',
        promoteUserId: '',
    },
    onLoad() {
      my.setNavigationBar({
        backgroundColor: '#1664FF'
      });
      app._init_(this)
      this.getUser();
      this.getHome();
    },
    onShow(){
      // this.getUser();
      this.getMarkNum();
      this.getList();
    },


    btn(){
      let flowId = '00b2b1ea07b64aaab8dc55299a550d27';
      my.navigateTo({
        url: `plugin://esign/home?flowId=${flowId}`,
      })
    },



    getHome(){
        app.other.home()
        .then(res=>{
            let eventsGoods = JSON.parse(res.data.eventsGoods)
            console.log(eventsGoods)
            let prudects =eventsGoods.prudects
            this.setData({prudects})
        })
    },



    // 获取列表
  getList(){

    if(!this.data.login){
      return;
    }

    let newDate = new Date();
    
    let year = newDate.getFullYear();
    let month = newDate.getMonth() + 1;

    let thisMonthDay = new Date(year, month, 0).getDate();
    console.log(thisMonthDay)

    let beforTimer = new Date(year+'/'+month+'/01').getTime();
    let yearTimer = new Date(year+'/'+month+'/' + thisMonthDay).getTime();


    let startTime = Math.floor(beforTimer/1000) + '';
    let endTime = Math.floor(yearTimer/1000) + '';

    let data = {
      startPeriodStartDate: startTime,
      startPeriodEndDate: endTime,
    }
    app.order.monthList(data)
    .then(res=>{
      if(res.code == 200 && res.data.length>0){
        res.data[0].expiretime = upTime("%M月%D日",res.data[0].expiretime);
        this.setData({
          recentlyBill: res.data[0]
        })
      }else{
        this.setData({
          recentlyBill: null
        })
      }
    })


  },






    // login
    getUser(){
        let user = my.getStorageSync({ key: 'user' });
        // 判断有没有登录
        if (user.data) {
            console.log("user", user)
            this.setData({
                user_header: user.data.header,
                user_name: user.data.uname,
                promoteId: user.data.promoteId,
                promoteUserId: user.data.promoteUserId,
                loginShow: false,
                login:true
            })
        } else {
            this.setData({
                user_header: '',
                user_name: '',
                promoteId: '',
                promoteUserId: '',
                loginShow: true,
                login:false,
                recentlyBill: null
            })
        }
    },
    login(){
        this.setData({
            loginShow:true
        })
    },

    onLgin(data){
      console.log(111,data)
      this.setData(data);
      this.getMarkNum();
      this.getList();
      this.getPromoteId();
    },


    getPromoteId(){
      let user = my.getStorageSync({ key: 'user' });
      if (user.data) {
          console.log("user", user)
          this.setData({
              promoteId: user.data.promoteId,
              promoteUserId: user.data.promoteUserId,
          })
      } else {
          this.setData({
              promoteId: '',
              promoteUserId: '',
          })
      }
    },

    logout(){
      let user = my.getStorageSync({ key: 'user' });
      if(!user.data){
        return;
      }
      this.setData({
          user_header: '',
          user_name: '',
          promoteId: '',
          promoteUserId: '',
          // loginShow: true,
          login:false,
          recentlyBill: null
      })
      my.clearStorage();

      this.resetMark();
      
      my.showToast({
          type: 'success',
          content: '退出成功',
          duration: 1000,
      });
    },






// 我的服务
    // 跳转
    navigateTo(e){
        app.navigateTo(e)
    },
    // 地址
    address(){
		let userInfo = my.getStorageSync({ key: 'user' })
		if (!userInfo.data) {
		    my.showToast({
		        type: 'exception ',
		        content: '你还没有登录',
		        duration: 1000,
		    });
		    return
		}
         my.getAddress({
            success: (res) => {
                // this.setData({
                //     tit: res.result.fullname,   // 姓名
                //     province: res.result.prov,   // 省
                //     city: res.result.city,        //市
                //     district: res.result.area,   // 区
                //     street: res.result.street,   //路
                //     address: res.result.address, //地址
                //     phone: res.result.mobilePhone, //电话
                // });
            },
            fail:()=>{
               
            }
        });
    },
    // 在线客服-电话
    server(){
        my.makePhoneCall({ number: app.globalData.number });
    },
    // 获取角标
    getMarkNum(){
      if(!this.data.login){
        return;
      }
      app.user.queryOrderStatusNum()
      .then(res=>{
        console.log(res.data);
        let arr = this.data.order;
        arr[0].markNum = res.data.noPayStatusNum;
        arr[1].markNum = res.data.noDeliverStatusNum;
        arr[2].markNum = res.data.noSignStatusNum;
        this.setData({
          order: arr
        })
      })
      .catch(()=>{
        this.resetMark();
      })
    },
    // 重置角标
    resetMark(){
      let arr = this.data.order;
      arr.forEach(item => {
        item.markNum = 0;
      });
      this.setData({
        order: arr
      })
    }
    
});
